package com.hao.wms.mvc;

import com.hao.wms.domain.StockOutcomeBill;
import com.hao.wms.query.StockOutcomeBillQueryObject;
import com.hao.wms.service.IClientService;
import com.hao.wms.service.IDepotService;
import com.hao.wms.service.IStockOutcomeBillService;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.IOException;

public class StockOutcomeBillAction extends BaseAction{
	/**
	 * 
	 */
	private IStockOutcomeBillService stockOutcomeBillService;
	private IDepotService depotService;
	private IClientService clientService;

	public void setClientService(IClientService clientService) {
		this.clientService = clientService;
	}

	private StockOutcomeBill stockOutcomeBill = new StockOutcomeBill();
	
	public StockOutcomeBill getStockOutcomeBill() {
		return stockOutcomeBill;
	}
	public void setStockOutcomeBill(StockOutcomeBill stockOutcomeBill) {
		this.stockOutcomeBill = stockOutcomeBill;
	}
	private StockOutcomeBillQueryObject queryObject = new StockOutcomeBillQueryObject();
	
	
	public StockOutcomeBillQueryObject getQueryObject() {
		return queryObject;
	}
	public void setQueryObject(StockOutcomeBillQueryObject queryObject) {
		this.queryObject = queryObject;
	}
	public void setStockOutcomeBillService(IStockOutcomeBillService stockOutcomeBillService) {
		this.stockOutcomeBillService = stockOutcomeBillService;
	}

	public void setDepotService(IDepotService depotService) {
		this.depotService = depotService;
	}

	@InputConfig(methodName = "input")
	@RequiredPermission(value="到货入库单列表")
	public String execute() {
		putContext(PAGERESULT, stockOutcomeBillService.query(queryObject));
		putContext("clients",clientService.list());
		putContext("depots",depotService.list());
		return LIST;
	}
	@RequiredPermission(value="新增/修改到货入库单")
	public String input() {
		putContext("depots",depotService.list());
		putContext("clients",clientService.list());
		if(stockOutcomeBill.getId()!=null) {
			stockOutcomeBill = stockOutcomeBillService.get(stockOutcomeBill.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改到货入库单")
	public String saveOrUpdate() {
		try {
			if(stockOutcomeBill.getId()!=null) {
                stockOutcomeBillService.update(stockOutcomeBill);
                addActionMessage("修改成功");
            }else {
                stockOutcomeBillService.save(stockOutcomeBill);
                addActionMessage("保存成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	public void prepareSaveOrUpdate(){
		if(stockOutcomeBill.getId()!=null) {
			stockOutcomeBill = stockOutcomeBillService.get(stockOutcomeBill.getId());
			stockOutcomeBill.setDepot(null);
			stockOutcomeBill.setClient(null);
			stockOutcomeBill.getItems().clear();
		}
	}
	@RequiredPermission(value="删除到货入库单")
	public String delete() throws IOException{
		stockOutcomeBill = stockOutcomeBillService.get(stockOutcomeBill.getId());
		stockOutcomeBillService.delete(stockOutcomeBill);
		putResponseText("删除成功");
		return NONE;
	}
	@RequiredPermission(value = "出库单审核")
	public String audit(){
		try {
			if(stockOutcomeBill.getId()!=null){
                stockOutcomeBillService.audit(stockOutcomeBill.getId());
                addActionMessage("审核成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}
	
	

}
